<?php

namespace app\home\controller;

use app\home\service\OrderService;
use think\Cache;
use think\Config;
use think\Controller;
use think\Loader;
use think\Request;

class BasePay extends Controller
{

    public $order_array = null;
    public $params = [];
    public function __construct()
    {
        //防止重复回调订单处理不及时导致重复发货
        if(request()->action() == 'notify'){
            $params = Request::instance()->param();
            //防止同一用户统一时间回传数据被覆盖
            $userCache = Cache::get(base64_encode(json_encode($params)));
            if($userCache){
                mylog('重复回调处理不及时');
                mylog($params);
                exit("FAIL repeat");
            }
            Cache::set(base64_encode(json_encode($params)),true,10);       
        }
    }

    public function _initialize()
    {
        parent::_initialize();
    }

    private function _setTestServer()
    {
        // 动态修改配置 为116服务器
        Config::set('db_config2.hostname', '116.62.143.6');
        Config::set('db_config2.username', 'root');
        Config::set('db_config2.password', 'amt#1@3!a$d%1^35a');

        Config::set('db_qpplatformdb.hostname', '116.62.143.6');
        Config::set('db_qpplatformdb.username', 'root');
        Config::set('db_qpplatformdb.password', 'amt#1@3!a$d%1^35a');

        Config::set('REDIS_FORMAL.host', '116.62.143.6');
        Config::set('REDIS_FORMAL.port', '6380');
        Config::set('REDIS_FORMAL.auth', 'hmYX!@#456');

        Config::set('HTTP_URL_TEST', 'http://116.62.143.6:10000/');
        Config::set('HTTP_URL_FORMAL', 'http://116.62.143.6:10000/');
        Config::set('HTTP_URL_FORMAL_GROUP_B', 'http://116.62.143.6:10000/');
    }

    // 下单
    public function order()
    {
        mylog("创建订单");
        $params = Request::instance()->param();
        $validate = Loader::validate('CreateOrder');
        if(!$validate->check($params)){
            $error = $validate->getError();
            mylog("创建失败:".$error);
            puterror($error);
        }

        if (strlen($params['userid']) == 5) {
            $this->_setTestServer();
        }

        $orderService = new OrderService;
        $result = $orderService->createOrder($params);
        if($result === false){
            puterror($orderService->getError());
        }

        $this->order_array = $result;
    }

    // 回调
    public function notify()
    {
        mylog("处理回调");
        $params = Request::instance()->param();
        if(!empty($params)){
            $this->params = $params;
            mylog($this->build_query($params));
        }else{
            $params = Request::instance()->getInput();
            if(!empty($params)){
                mylog($params);
                $arr = explode('&', $params);
                foreach($arr as $v){
                    $arr = explode('=',$v);
                    $this->params[$arr[0]] = urldecode($arr[1]);
                }
            }else{
                mylog("参数为空");
                puterror("参数为空");
            }
        }
    }


    /**
     * 内置的http_build_query会做转义 不利于查看日志 自定义数组分割
     * @param $data
     * @return string
     */
    private function build_query($data){
        $string = "";
        if(empty($data)){
            return $string;
        }
        if(is_array($data)){
            foreach($data as $k=>$v){
                $string .= "{$k}={$v}&";
            }
        }

        return rtrim($string,"&");
    }
}
